/**
 * 
 */
package base.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Administrator
 * 
 */
public class TestServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5222793251610509039L;
	
	static Connection con = null;
	static String url = "jdbc:mysql://localhost:3306/test";

	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			con = DriverManager.getConnection(url, "root", "liuboen");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String ipAddr = getIpAddr(request);
		long ip = getIPNum(ipAddr);
		//String serverAddr= InetAddress.getLocalHost().getHostAddress();
		String province = getIpAddr(ip);
		request.setAttribute("province", province);
		request.setAttribute("ipAddr", ipAddr);

		RequestDispatcher dispatcher = request.getRequestDispatcher("/web/default/screen/mytest.jsp");
		dispatcher.forward(request, response);
		// response.sendRedirect("/web/default/screen/mytest.jsp");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doGet(req, resp);
	}

	public String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		System.out.println("remote host=" + ip);
		return ip;
	}

	public String getIpAddr(long ip) {
		String province = "���ص���";
		String city = "";
		try {
			String sql = "SELECT * from trade_logistics_city_ip where START_IP<= ? AND END_IP>=?";
			PreparedStatement p = con.prepareStatement(sql);
			p.setLong(1, ip);
			p.setLong(2, ip);
			ResultSet rs = p.executeQuery();
			while (rs.next()) {
				province = rs.getString("province");
				city = rs.getString("city");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		String area = new StringBuilder(province).append(city).toString();
		System.out.println("come from��" + area);
		return area;
	}

	/**
	 * ��ȡIP��ַ����
	 * 
	 * @param ip
	 * @return
	 */
	public long getIPNum(String ip) {
		String[] ipMoudles = ip.split("\\.");
		long ipA = Long.parseLong(ipMoudles[0]);
		long ipB = Long.parseLong(ipMoudles[1]);
		long ipC = Long.parseLong(ipMoudles[2]);
		long ipD = Long.parseLong(ipMoudles[3]);

		long ipLong = (ipA * 256 * 256 * 256) + (ipB * 256 * 256) + (ipC * 256) + ipD;
		return ipLong;
	}

}
